﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Atomic.Common
{
    /// <summary>
    /// 系统通用扩展
    /// </summary>
    public static class SystemExtend
    {
        /// <summary>
        /// 将字符串转换为整型
        /// </summary>
        /// <param name="obj">待转换的字符串</param>
        /// <returns></returns>
        public static int ToInt32(this string obj)
        {
            return int.Parse(obj);
        }

        /// <summary>
        /// 将字符串转换为整型，失败则转化为指定值。不会抛出异常。
        /// </summary>
        /// <param name="obj">待转换的字符串</param>
        /// <param name="defaultValue">转化失败返回的默认值</param>
        /// <returns></returns>
        public static int ToInt32(this string obj, int defaultValue)
        {
            int tmp = 0;
            if (int.TryParse(obj, out tmp))
            {
                return tmp;
            }
            else
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 用指定的分隔符将数组分割为字符串格式
        /// </summary>
        /// <param name="list">对象数组</param>
        /// <param name="splitor">分隔符</param>
        /// <returns>分隔符分隔的字符串</returns>
        public static string ToSplitString<T>(this IList<T> list, string splitor)
        {
            if (list.Count == 0)
            {
                return string.Empty;
            }
            else
            {
                StringBuilder builder = new StringBuilder();
                foreach (object item in list)
                {
                    builder.Append(item.ToString()).Append(splitor);
                }
                if (builder.Length > 0)
                {
                    builder.Remove(builder.Length - splitor.Length, splitor.Length);    //删除最后的分隔符字符串
                }
                return builder.ToString();
            }
        }

        #region 数据库部分
        
        /// <summary>
        /// 分割为SQL语句中Insert Values 中的内容
        /// </summary>
        /// <param name="list">对象数组</param>
        /// <returns>Values部分字符串</returns>
        public static string ToSQLInsertValues<T>(this IList<T> list)
        {
            if (list.Count == 0)
            {
                return string.Empty;
            }
            else
            {
                StringBuilder builder = new StringBuilder();
                foreach (object item in list)
                {
                    builder.Append("'").Append(item.ToString()).Append("',");
                }
                if (builder.Length > 0)
                {
                    builder.Remove(builder.Length - 1, 1);    //删除最后的分隔符字符串
                }
                return builder.ToString();
            }
        }

        /// <summary>
        /// 分割为SQL语句中Update Set 中的内容
        /// </summary>
        /// <param name="list">对象数组</param>
        /// <returns>Set部分字符串</returns>
        public static string ToSQLUpdateValues(this IList<KeyValuePair<string,object>> list)
        {
            if (list.Count == 0)
            {
                return string.Empty;
            }
            else
            {
                StringBuilder builder = new StringBuilder();
                foreach (KeyValuePair<string,object> item in list)
                {
                    builder.Append(item.Key).Append("='").Append(item.Value.ToString()).Append("',");
                }
                if (builder.Length > 0)
                {
                    builder.Remove(builder.Length - 1, 1);    //删除最后的分隔符字符串
                }
                return builder.ToString();
            }
        }

        #endregion
    }
}
